<?php
/**
 * 费用
 */
declare (strict_types = 1);
namespace app\table\service;

use app\core\Relation;
use app\core\Res;
use think\facade\Db;

class ActionService
{


    /**
     * 费用列表
     * @param $params
     * @return $data
     */
    public static function list(array $params,$export=0){
        $model= Relation::getModel('table_action');
        $queryField=[
            'depart_id'=>'=',
            'worker_id'=>'=',
        ];
        $map=[];

        $createDateSel=Res::kv($params,'createDateSel');
        if($createDateSel!=''){
            $dtlist=explode(' - ', $createDateSel);
            $start_time=Res::kv($dtlist,0);
            $end_time=Res::kv($dtlist,1);
            $map[] = ['cdate','>=',$start_time];
            $map[] = ['cdate','<=',$end_time];
        }

        

        $map=Res::querymap($model,$map,$queryField,$params);

        $keywords=Res::kv($params,'keywords');
        if($keywords!=''){
            $map[] = ['action_name|action_name_zh','like',"%".$keywords."%"];
        }

        $model=$model->where($map);


        //导出数据
        if($export==1){
            $list=$model->select();
            $tmp=[];
            foreach($list as $key=>$v){
                $tmp[$key]=$v;
            }
            return $tmp;
        }
        //分页数据显示
        $data= Res::page($model,$params);

        $tmp=[];
        foreach($data['data'] as $key=>$v){
            $v['action_url']='/sk/index/index/mtz/'.$v['action_name'];
            $tmp[$key]=$v;
        }
        $data['data']=$tmp;

        return $data;
    }

    /**
     * 列表
     * @param $params
     * @return $data
     */
    public static function modelList(array $params,$export=0){
        $model= Relation::getModel('table_action_model');
        $queryField=[
            'action_id'=>'=',
        ];
        $map=[];

        $createDateSel=Res::kv($params,'createDateSel');
        if($createDateSel!=''){
            $dtlist=explode(' - ', $createDateSel);
            $start_time=Res::kv($dtlist,0);
            $end_time=Res::kv($dtlist,1);
            $map[] = ['cdate','>=',$start_time];
            $map[] = ['cdate','<=',$end_time];
        }

        

        $map=Res::querymap($model,$map,$queryField,$params);

        $keywords=Res::kv($params,'keywords');
        if($keywords!=''){
            $map[] = ['table_name|table_name_zh','like',"%".$keywords."%"];
        }

        $model=$model->where($map);


        
        //分页数据显示
        $data= Res::page($model,$params);

        $tmp=[];
        foreach($data['data'] as $key=>$v){
            $tmp[$key]=$v;
        }
        $data['data']=$tmp;

        return $data;
    }

    /**
     * 列表
     * @param $params
     * @return $data
     */
    public static function modelFieldList(array $params,$export=0){
        $model= Relation::getModel('table_action_field');
        $queryField=[
            'action_id'=>'=',
        ];
        $map=[];

        $createDateSel=Res::kv($params,'createDateSel');
        if($createDateSel!=''){
            $dtlist=explode(' - ', $createDateSel);
            $start_time=Res::kv($dtlist,0);
            $end_time=Res::kv($dtlist,1);
            $map[] = ['cdate','>=',$start_time];
            $map[] = ['cdate','<=',$end_time];
        }

        

        $map=Res::querymap($model,$map,$queryField,$params);

        $keywords=Res::kv($params,'keywords');
        if($keywords!=''){
            $map[] = ['field_name_zh|table_name_zh','like',"%".$keywords."%"];
        }

        $model=$model->where($map);


        
        //分页数据显示
        $data= Res::page($model,$params);

        $tmp=[];
        foreach($data['data'] as $key=>$v){
            $tmp[$key]=$v;
        }
        $data['data']=$tmp;

        return $data;
    }

    /**
     * 列表
     * @param $params
     * @return $data
     */
    public static function TitleList(array $params,$export=0){
        $model= Relation::getModel('table_action_title');
        $queryField=[
            'action_id'=>'=',
        ];
        $map=[];

        $createDateSel=Res::kv($params,'createDateSel');
        if($createDateSel!=''){
            $dtlist=explode(' - ', $createDateSel);
            $start_time=Res::kv($dtlist,0);
            $end_time=Res::kv($dtlist,1);
            $map[] = ['cdate','>=',$start_time];
            $map[] = ['cdate','<=',$end_time];
        }

        

        $map=Res::querymap($model,$map,$queryField,$params);

        $keywords=Res::kv($params,'keywords');
        if($keywords!=''){
            $map[] = ['title_name|title_name_zh','like',"%".$keywords."%"];
        }

        $model=$model->where($map);


        
        //分页数据显示
        $data= Res::page($model,$params);

        $tmp=[];
        foreach($data['data'] as $key=>$v){
            $tmp[$key]=$v;
        }
        $data['data']=$tmp;

        return $data;
    }

    /**
     * 添加编辑
     * @param $params
     * @return $data
     */
    public static function addAction(array $params){
        $redata=[];
        $msg='';

        $action_name=Res::kv($params,'action_name');
        $action_name_zh=Res::kv($params,'action_name_zh');
        $action_type=Res::kv($params,'action_type');
        $action_type_name=Res::getConfig('cfg.action',$action_type);
        $id=Res::kv($params,'id');
        if($action_name=='' || $action_name_zh==''){
            $msg.='[请输入名称]';
        }
        $model=Relation::getModel('table_action');

        $find=Res::findData($model,['action_name'=>$action_name],'id');
        if($find){
            if($id==''){
                $msg.='[请求名称已存在]';
            }else{
                if($id!=$find['id']){
                    $msg.='[请求名称已存在]';
                }
            }
        }
        if($msg!=''){
            $redata=['code'=>0,'msg'=>$msg,'data'=>[]];
            return $redata;
        }
        $saveData=[
            'action_name'=>$action_name,
            'action_type'=>$action_type,
            'action_type_name'=>$action_type_name,
            'action_name_zh'=>$action_name_zh
        ];
        
        Db::startTrans();
        try {
           
            $res=Res::saveModelData($model,$saveData,['action_name'=>$action_name]);
            if($res>0){
                Db::commit();
                return  ['code'=>1,'msg'=>'数据保存成功','data'=>[]];
            }else{
                Db::rollback();
                return  ['code'=>0,'msg'=>'数据保存失败,请查看项目是否输入完整','data'=>[]];
            }
        }catch (\PDOException $e){
            Db::rollback();
            return  ['code'=>0,'msg'=>'数据保存失败','data'=>[]];
        }
    }


    /**
     * 添加编辑
     * @param $params
     * @return $data
     */
    public static function addModel(array $params){
        $redata=[];
        $msg='';
        

        $ids=Res::kv($params,'ids');
        $table_id=Res::kv($params,'table_id');

        $map=[];
        $map[] = ['id','in',$ids];
        $data=Res::getDataByIds('table_list',$map,'*');

        
        if(!$data){
            $msg.='[请选择表]';
        }
        if($msg!=''){
            $redata=['code'=>0,'msg'=>$msg,'data'=>[]];
            return $redata;
        }
        
        foreach($data as $items){
            self::addModelRow($items,$table_id);
        }
        return  ['code'=>1,'msg'=>'数据保存成功','data'=>[]];
    }

    /**
     * 添加编辑
     * @param $params
     * @return $data
     */
    public static function addModelRow($data,$action_id){
        
        $saveData=[
            'action_id'=>$action_id,
            'table_id'=>$data['id'],
            'table_name'=>$data['table_name'],
            'table_name_zh'=>$data['table_name_zh'],
        ];
        $model=Relation::getModel('table_action_model');
        Db::startTrans();
        try {
            $res=Res::saveModelData($model,$saveData,['action_id'=>$action_id,'table_id'=>$data['id']]);
            self::addModelField($data,$action_id);
            if($res>0){
                Db::commit();
            }else{
                Db::rollback();
            }
        }catch (\PDOException $e){
            Db::rollback();
        }
    }

    /**
     * 添加编辑
     * @param $params
     * @return $data
     */
    public static function addModelField($data,$action_id){
        $map=[];
        $map[] = ['table_id','in',$data['id']];
        $fieldData=Res::getDataByIds('table_field',$map,'*');
        $saveData=[
            'action_id'=>$action_id,
            'table_id'=>$data['id'],
            'table_name'=>$data['table_name'],
            'table_name_zh'=>$data['table_name_zh'],
        ];
        if($fieldData){
            foreach($fieldData as $items){
                $saveData['field_id']=$items['id'];
                $saveData['field_name']=$items['field_name'];
                $saveData['field_name_zh']=$items['field_name_zh'];
                $saveData['field_type_id']=$items['type_id'];
                self::addModelFielditems($saveData);
            }
        }
        
       
    }

    /**
     * 添加编辑
     * @param $params
     * @return $data
     */
    public static function addModelFielditems($data){
        
        $model=Relation::getModel('table_action_field');
        Db::startTrans();
        try {
            $res=Res::saveModelData($model,$data,['action_id'=>$data['action_id'],'field_id'=>$data['field_id']]);
            if($res>0){
                Db::commit();
            }else{
                Db::rollback();
            }
        }catch (\PDOException $e){
            Db::rollback();
        }
    }

    /**
     * 添加编辑
     * @param $params
     * @return $data
     */
    public static function addTitle(array $params){
        $redata=[];
        $msg='';
        $action_id=Res::kv($params,'actionid');
        $id='';
        if($action_id==''){
            $action_id=Res::kv($params,'action_id');
            $id=Res::kv($params,'id');
        }
        $title_name=Res::kv($params,'title_name');
        $title_name_zh=Res::kv($params,'title_name_zh');
        $title_url=Res::kv($params,'title_url');
        $title_type=Res::kv($params,'title_type');
        $title_type_name=Res::getConfig('cfg.actiontitle',$title_type);

        $title_type_act=Res::kv($params,'title_type_act');
        $title_type_act_name=Res::getConfig('cfg.actiontitletype',$title_type_act);
        
        if($title_name=='' || $title_type_name=='' || $action_id==''){
            $msg.='[请输入名称]';
        }
        $model=Relation::getModel('table_action_title');

        $find=Res::findData($model,['title_name'=>$title_name,'action_id'=>$action_id],'id');
        if($find){
            if($id==''){
                $msg.='[请求名称已存在]';
            }else{
                if($id!=$find['id']){
                    $msg.='[请求名称已存在]';
                }
            }
        }
        if($msg!=''){
            $redata=['code'=>0,'msg'=>$msg,'data'=>[]];
            return $redata;
        }
        $saveData=[
            'action_id'=>$action_id,
            'title_name'=>$title_name,
            'title_url'=>$title_url,
            'title_type'=>$title_type,
            'title_type_name'=>$title_type_name,
            'title_type_act'=>$title_type_act,
            'title_type_act_name'=>$title_type_act_name,
            'title_name_zh'=>$title_name_zh
        ];
        
        Db::startTrans();
        try {
            if($id!=''){
                $res=Res::saveModelData($model,$saveData,['id'=>$id]);
            }else{
                $res=Res::saveModelData($model,$saveData,['title_name'=>$title_name,'action_id'=>$action_id]);
            }
            
            if($res>0){
                Db::commit();
                return  ['code'=>1,'msg'=>'数据保存成功','data'=>[]];
            }else{
                Db::rollback();
                return  ['code'=>0,'msg'=>'数据保存失败,请查看项目是否输入完整','data'=>[]];
            }
        }catch (\PDOException $e){
            Db::rollback();
            return  ['code'=>0,'msg'=>'数据保存失败','data'=>[]];
        }
    }

    /**
     * 添加编辑
     * @param $params
     * @return $data
     */
    public static function tbStatus(array $params){
        
        $id=Res::kv($params,'id');
        $selflag=Res::kv($params,'selflag');
        $status=Res::kv($params,'status');
        $data=[];
        if($selflag=='show'){
            $data['field_show']=$status;
        }elseif($selflag=='verify'){
            $data['field_verify']=$status;
        }elseif($selflag=='tableshow'){
            $data['field_tbshow']=$status;
        }elseif($selflag=='TableExport'){
            $data['field_export']=$status;
        }else{
            return  ['code'=>0,'msg'=>'状态更改失败','data'=>[]];
        }
        $model=Relation::getModel('table_action_field');
        Db::startTrans();
        try {
            $res=Res::saveModelData($model,$data,['id'=>$id]);
            if($res>0){
                Db::commit();
                return  ['code'=>1,'msg'=>'状态更改成功','data'=>[]];
            }else{
                Db::rollback();
                return  ['code'=>0,'msg'=>'状态更改失败','data'=>[]];
            }
        }catch (\PDOException $e){
            Db::rollback();
            return  ['code'=>0,'msg'=>'状态更改失败','data'=>[]];
        }
    }

    /**
     * 删除
     * @param $params
     * @return $data
     */
    public static function titleDel(array $params){
        $model=Relation::getModel('table_action_title');
        $id=Res::kv($params,'id',0);
        $map[] = ['id','=',$id];
        $map=Res::querymap($model,$map);
        $model->where($map)->delete();
        return [];
    }

    
}